<template>
  <!-- 添加或修改用户试卷对话框 -->
  <el-dialog :visible.sync="visible" :title="modalObj.title" :width="modalObj.width" :modal='modalObj.modal'
             center :close-on-click-modal="true"
             :close-on-press-escape="true"
             @closed="cancel"
             append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">

      <el-form-item label="试卷" prop="paper">
        <el-select v-model="form.paper" placeholder="请输入试卷">
          <el-option v-for="p in paperList" :label="p.title" :value="p.id"></el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="状态" v-if="form.id">
        <el-radio-group v-model="form.status">
          <el-radio
            v-for="dict in dict.type.el_paper_status"
            :key="dict.value"
            :label="dict.value"
          >{{dict.label}}
          </el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listPaper } from '@/api/exam/paper'
import { getUserPaper, addUserPaper, updateUserPaper } from '@/api/exam/userPaper'
import { mapGetters } from 'vuex'

export default {
  name: 'UserPaperEdit',
  dicts: ['el_paper_status'],

  props: {
    visible: {
      type: Boolean,
      default: false
    },
    modalObj: {
      type: Object,
      default: false
    }
  },
  computed: {
    ...mapGetters([
      'userLevel',
      'posts'
    ])
  },
  data() {
    return {
      paperList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        paper: [
          { required: true, message: '试卷不能为空', trigger: 'blur' }
        ]
      },
      queryParams: {
        status: '1'
      }
    }
  },
  created() {
    this.reset()
    this.getData()
  },
  methods: {
    /** 查询用户试卷列表 */
    getData() {
      if (this.modalObj.id) {
        getUserPaper(this.modalObj.id).then(response => {
          this.form = response.data
        })
      }
      // if (this.posts.includes('exam')) {
      console.log("=======================qu")
      console.log(this.queryParams)
        listPaper(this.queryParams).then(response => {
          console.log("=====================考试")
          console.log(response)
          this.paperList = response.rows
        })
      // }
    },
    // 取消按钮
    cancel() {
      this.$emit('handleClose', { component: 'openEdit' })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        paper: null,
        status: '0'
      }
      this.resetForm('form')
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUserPaper(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.$emit('handleClose', { component: 'openEdit', get: true })
            })
          } else {
            addUserPaper(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.$emit('handleClose', { component: 'openEdit', get: true })
            })
          }
        }
      })
    }

  }
}
</script>
